package com.auanaum.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.auanaum.dto.Movie;
import com.auanaum.sevice.MovieService;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;

@Controller
@RequestMapping("/movie")
public class MovieController {
	 
		 
	@Autowired
    private MovieService movieService;

    @Autowired
    private UserService userService;
	
	@RequestMapping(value="/{name}", method = RequestMethod.GET)
	public String getGuest(@PathVariable String name, ModelMap model) {	
		User user = userService.getCurrentUser();
        model.addAttribute("user", user);
        model.addAttribute("loginHref", userService.createLoginURL("/movie/new"));
        model.addAttribute("logoutHref", userService.createLogoutURL("/movie/prueb"));
        model.addAttribute("movies", this.movieService.findAllMovies());

       return "m";  
 
	}
	
	@RequestMapping(value="/new", method=RequestMethod.POST)
    public String create(@RequestParam("content") String content, ModelMap model) {
        User user = userService.getCurrentUser();
        String val ="prueba";
        Date date = new Date();
        Movie movie = new Movie(user, content, val, date);
        movieService.addMovie(movie);

        return getGuest("prueba", model);
    }

    
}